package com.atguigu.stack;

import java.util.Scanner;

public class ArrayStackDemo {

    public static void main(String[] args) {
        ArrayStack2 arrayStack2 = new ArrayStack2(5);
        int num = -1;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你的选择：1.显示栈 2.添加数据到栈 3.取出数据 4.退出");
            num = scanner.nextInt();
            switch (num) {
                case 1:
                    arrayStack2.show();
                    break;
                case 2:
                    System.out.println("请输入一个数");
                    int n = scanner.nextInt();
                    arrayStack2.push(n);
                    break;
                case 3:
                    try {
                        int result = arrayStack2.pop();
                        System.out.println("取出的数据为：" + result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 4:
                    scanner.close();
                    System.out.println("程序已退出");
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }
}

class ArrayStack {
    private int maxSize;
    private int top = -1;
    private int[] stack;//


    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public int[] getStack() {
        return stack;
    }

    public void setStack(int[] stack) {
        this.stack = stack;
    }

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈空，没有数据");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }


}
